import {defineStore} from "pinia"
// 引入完整的动态路由表
import routes from "../router/generatorRoutes"
import * as api from "../api/permission"
import recursionRoutes from "../utils/recursionRoutes"
// 引入动态路由
import dynamicRoutes from "../router/dynamicRoutes"
// 引入静态路由
import staticRoute from "../router/staticRoute"
// 引入路由对象
import router from "../router"

export const usePermission=defineStore('permission',{
    state(){
        return{
            menuList:[]
        }
    },
    actions:{
        fetch_menulist(){
            //发起请求获取菜单
            return api.getMenuApi()
            .then(res=>{
                // 已经获取了用户的路由
                let userRoutes=recursionRoutes(res.data.menuList,routes)
                this.menuList=userRoutes
                // 开始添加动态路由
                // 1.先从动态路由中找到路径是是'/'的对象
                let target =dynamicRoutes.find(route=>route.path==="/")
                // 2.将用户路由变成他的孩子
                target.children=userRoutes
                // 3.静态路由也要添加到dynamicRoutes的孩子里面
                target.children=[...target.children,...staticRoute]
                // 4.使用addRoute方法，将dynamicRoutes添加到路由的routes配置中
                router.addRoute(target)
            })
        }
    },
    getters:{

    }
})